<template>
  <div class="department-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" :root="true" />

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <TreeTools
              :node-data="data"
              @add-depts="addDepts"
              @del-depts="getDepartments"
              @edit-depts="editDepts"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDepts
        ref="addDepts"
        :is-show="dialogVisible"
        :node-data="nodeData"
        :dept-list="departsList"
        @add-dept="handleClose"
        @handleClose="handleClose"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import { reqGetDepartments } from '@/api/department'
import { transListToTree } from '@/utils/index' // 引入列表转树形数据
import AddDepts from '@/views/departments/components/AddDepts.vue'
export default {
  name: 'Department',
  components: { TreeTools, AddDepts },
  data() {
    return {
      departs: [], // 树形数据
      departsList: [], // 列表数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      dialogVisible: false, // 控制弹出层显隐
      // 存储正在操作的弹窗数据
      nodeData: {},
      loading: false // 控制loading
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const {
        data: { depts }
      } = await reqGetDepartments()
      this.departsList = depts
      console.log(depts)
      // 返回的一级部门列表 pid === ""
      const res = transListToTree(depts, '')
      this.departs = res
      this.loading = false
    },
    handleClose(value) {
      this.dialogVisible = value
      // 刷新数据
      this.getDepartments()
    },
    addDepts(value) {
      // console.log('增加部门', value)
      this.dialogVisible = true
      this.nodeData = value
      this.$refs.addDepts.title = '添加子元素'
    },
    editDepts(nodeData) {
      // console.log('编辑', value)
      this.dialogVisible = true
      this.nodeData = nodeData
      this.$nextTick(() => {
        this.$refs.addDepts.getGetDepartDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>

